var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    argeen: false,
    inquiry: "1", // "" 家庭医生支付  ！"" 问诊
    switchChecked: false,
    money: "",
    tid: "",
    key_words: "",
    addnum: "",
    ser_key: "",
    pinyin: "",
    y_money: "",
    coupon_id: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var tid = options.tid;
    var key_words = options.key_words;
    var addnum = options.addnum;
    var ser_key = options.ser_key;
    var pinyin = options.pinyin;
    var money = options.money;
    that.setData({
      tid: tid,
      key_words: key_words,
      addnum: addnum,
      ser_key: ser_key,
      pinyin: pinyin,
      money: money,
      y_money: money,
      j_id: options.j_id
    })
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    if (options.inquiry != "undefined") {
      this.setData({
        inquiry: options.inquiry
      })
    }
    that.getDetail();
  },
  getDetail: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/team.order_detail",
      data: {
        id: that.data.tid,
        key_words: that.data.key_words
      },
      success: function (res) {
        that.setData({
          info: res.data
        })
      }
    })
  },
  // 是否同意协议
  radioChange() {
    this.setData({
      argeen: !this.data.argeen
    })
  },
  //公开 开关
  switchChange() {
    this.setData({
      switchChecked: !this.data.switchChecked
    })
  },
  //支付按钮
  paybtn() {
    var that = this;
    var openid = wx.getStorageSync('openid');
    var tid = that.data.tid;
    var money = that.data.money;
    var key_words = that.data.key_words;
    wx.showModal({
      title: '是否支付',
      success: function (res) {
        if (res.confirm) {

          if (money == '0.00') {
            var ifpay = 1
          } else {
            var ifpay = 0
          }
          app.util.request({
            'url': 'entry/wxapp/team.orders',
            data: {
              tid: tid,
              money: money,
              openid: openid,
              start: that.data.info.start,
              end: that.data.info.end,
              coupon_id: that.data.coupon_id,
              j_id: that.data.j_id,
              y_money: that.data.y_money,
              key_words: key_words,
              addnum: that.data.addnum,
              ifpay: ifpay
            },
            success: function (res) {
              console.log(res)
              var orders = res.data.orders
              var back_orser = res.data.back_orser
              var id = res.data.id
              if (money == '0.00') {
                wx.showToast({
                  title: '签约成功',
                  icon: 'none',
                  duration: 2000
                })
                setTimeout(function () {
                  wx.navigateTo({
                    url: '/hyb_yl/userCommunicate/pages/recordSigning/recordSigning',
                  })
                }, 2000)

              } else {
                app.util.request({
                  'url': 'entry/wxapp/team.paywenzhen',
                  header: {
                    "Content-Type": "application/xml"
                  },
                  method: "GET",
                  data: {
                    openid: wx.getStorageSync('openid'),
                    z_tw_money: money,
                    orders: orders,
                    key_words: key_words
                  },
                  success: function (res) {
                    console.log(res);
                    wx.requestPayment({
                      'timeStamp': res.data.timeStamp,
                      'nonceStr': res.data.nonceStr, //字符串随机数
                      'package': res.data.package,
                      'signType': res.data.signType,
                      'paySign': res.data.paySign,
                      success: function (res) {
                        console.log(res)
                        // wx.navigateTo({
                        //   url: '../zhifuend/zhifuend?txt=yes&tid=' + that.data.tid + '&back_orser=' + back_orser + '&key_words=' + key_words + '&j_id=' + that.data.j_id,
                        // })
                        wx.showToast({
                          title: '签约成功',
                          icon: 'none',
                          duration: 2000
                        })
                        setTimeout(function () {
                          wx.navigateTo({
                            url: '/hyb_yl/userCommunicate/pages/recordSigning/recordSigning',
                          })
                        }, 2000)
                      },
                      fail: function (res) {
                        console.log(res)
                      }
                    })
                  }
                })
              }
              app.util.request({
                'url': 'entry/wxapp/team.getdocmbtxing',
                data: {
                  id: id,
                  val: key_words,
                  near: key_words,
                  id: id
                },
                success: function (res) {
                  console.log(res)

                }
              })

            }
          })
        }
      }
    })
  },
  //预约时间
  orderedbtn() {
    wx.navigateTo({
      url: '../orderedtime/orderedtime',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})